/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.service.services.acl;

import opiam.admin.faare.config.javabeans.JBAcl;
import opiam.admin.faare.config.javabeans.JBAcls;
import opiam.admin.faare.exception.ServiceException;
import opiam.admin.faare.persistence.javabeans.JBTop;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.services.acl.AclPluginInterface;
import opiam.admin.faare.unittests.TestUtils;

import org.apache.log4j.Logger;


/**
 * Test plugin to use to test the acls.
 */
public class TestAclPlugin implements AclPluginInterface
{
    /** Set of the acl definitions. */
    private static JBAcls tacls = null;

    /** Test Acl. */
    private static JBAcl tacl = null;

    /** Acl name. */
    private static final String ACL_NAME = "test_acl";

    /** Modified attribute name. */
    private static final String ATTRIBUTE_NAME = "cn";

    /** Instance of the loger. */
    private static Logger _logger = Logger.getLogger(TestAclPlugin.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /**
     * Initializes the plugin.
     *
     * @param acls  Set of the acls definitions.
     *
     * @see opiam.admin.faare.service.services.acl.AclPluginInterface#initialize(JBAcls)
     */
    @Override
	public void initialize(JBAcls acls)
    {
        _test.displayMsg("Initialize the TestAclPlugin.");

        try
        {
            tacls = acls;
            tacl = (JBAcl) (tacls.getAclsMap()).get(ACL_NAME);
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("TestAclPlugin initialization error\n");
            msg.append(ex.toString());
            _test.displayResultFailure(msg.toString());
        }
    }

    /**
     * Indicates if the modification is enabled.
     *
     * @param arg0  Reference entry.
     * @param arg1  Entry with the modification.
     * @param userContext  Context of the user.
     *
     * @see opiam.admin.faare.service.services.acl.AclPluginInterface#
     *         isModificationEnabled(JBTop, JBTop, UserContext)
     * @return see opiam.admin.faare.service.services.acl.AclPluginInterface#
     *         isModificationEnabled(JBTop, JBTop, UserContext)
     * @throws ServiceException Not thrown.
     */
    @Override
	public boolean isModificationEnabled(JBTop arg0, JBTop arg1,
        UserContext userContext) throws ServiceException
    {
        if ((tacl != null) && (tacl.canModify(ATTRIBUTE_NAME)))
        {
            return true;
        }

        return false;
    }

    /**
     * Indicates if the visualisation is enabled.
     *
     * @param entry  Reference entry.
     * @param userContext  Context of the user.
     *
     * @see opiam.admin.faare.service.services.acl.AclPluginInterface#isVisualisationEnabled(JBTop, UserContext)
     * @return see opiam.admin.faare.service.services.acl.AclPluginInterfac
     * @throws ServiceException see opiam.admin.faare.service.services.acl.AclPluginInterfac
     */
    @Override
	public boolean isVisualisationEnabled(JBTop entry, UserContext userContext)
        throws ServiceException
    {
        if ((tacl != null) && (tacl.canVisualize()))
        {
            return true;
        }

        return false;
    }

    /**
     * Indicates if the creation is enabled.
     *
     * @param entry  Reference entry.
     * @param userContext  Context of the user.
     *
     * @see opiam.admin.faare.service.services.acl.AclPluginInterface#isCreationEnabled(JBTop, UserContext)
     * @return see opiam.admin.faare.service.services.acl.AclPluginInterfac
     * @throws ServiceException see opiam.admin.faare.service.services.acl.AclPluginInterfac
     */
    @Override
	public boolean isCreationEnabled(JBTop entry, UserContext userContext)
        throws ServiceException
    {
        if ((tacl != null) && (tacl.canCreate()))
        {
            return true;
        }

        return false;
    }

    /**
     * Indicates if the deletion is enabled.
     *
     * @param entry  Reference entry.
     * @param userContext  Context of the user.
     *
     * @see opiam.admin.faare.service.services.acl.AclPluginInterface#isDeletionEnabled(JBTop, UserContext)
     * @return see opiam.admin.faare.service.services.acl.AclPluginInterfac
     * @throws ServiceException see opiam.admin.faare.service.services.acl.AclPluginInterfac
     */
    @Override
	public boolean isDeletionEnabled(JBTop entry, UserContext userContext)
        throws ServiceException
    {
        if ((tacl != null) && (tacl.canDelete()))
        {
            return true;
        }

        return false;
    }
}
